14 Lecture

CS201

Midterm & Final Term Short Notes

Pointers

Pointers are variables that hold the memory addresses of other variables. They allow for dynamic memory allocation and manipulation of data structures. Pointers are commonly used in programming to pass arguments to functions, to allocate and dea


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a pointer in C++? a) A keyword used to declare variables b) A variable that stores the memory address of another variable c) A data type used to store text d) A type of loop used in programming

Answer: b) A variable that stores the memory address of another variable

  1. Which operator is used to declare a pointer variable in C++? a) * b) & c) $ d) #

Answer: a) *

  1. Which of the following statements is true about null pointers in C++? a) They point to the address 0x0000. b) They point to the address 0xFFFF. c) They are not allowed in C++. d) They can be used to indicate that a pointer does not point to anything.

Answer: d) They can be used to indicate that a pointer does not point to anything.

  1. What is the difference between a pointer and a reference in C++? a) A pointer is a variable that holds a memory address, while a reference is an alias for another variable. b) A reference is a variable that holds a memory address, while a pointer is an alias for another variable. c) There is no difference between a pointer and a reference. d) A pointer is used for dynamic memory allocation, while a reference is used for static memory allocation.

Answer: a) A pointer is a variable that holds a memory address, while a reference is an alias for another variable.

  1. What is the use of the new operator in C++? a) To declare a new variable b) To delete a variable c) To allocate memory for a new object d) To free memory for an existing object

Answer: c) To allocate memory for a new object

  1. What is the use of the delete operator in C++? a) To declare a new variable b) To delete a variable c) To allocate memory for a new object d) To free memory for an existing object

Answer: d) To free memory for an existing object

  1. What is a dangling pointer? a) A pointer that points to an invalid memory address b) A pointer that points to a valid memory address c) A pointer that points to a null value d) A pointer that points to an uninitialized value

Answer: a) A pointer that points to an invalid memory address

  1. Which of the following is the correct syntax to declare a pointer to a constant integer in C++? a) const int *ptr; b) int const *ptr; c) const int * const ptr; d) int * const ptr;

Answer: b) int const *ptr;

  1. What is a void pointer in C++? a) A pointer that points to a function b) A pointer that points to a structure c) A pointer that has no data type associated with it d) A pointer that points to an integer value

Answer: c) A pointer that has no data type associated with it

  1. What is the use of the reinterpret_cast operator in C++? a) To cast a pointer from one data type to another b) To cast a reference from one data type to another c) To cast a variable from one data type to another d) To cast a pointer from one function to another

Answer: a) To cast a pointer from one data type to another



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a pointer in C programming? A pointer is a variable that stores a memory address. It allows programmers to directly manipulate memory and is useful for efficient dynamic memory allocation.

  2. How do you declare a pointer variable in C? A pointer variable is declared by adding an asterisk (*) before the variable name, for example: int *ptr;

  3. What is the purpose of the ampersand (&) operator in C? The ampersand (&) operator is used to get the address of a variable in memory, for example: #

  4. How do you access the value pointed to by a pointer in C? You can access the value pointed to by a pointer by using the dereference operator (*) before the pointer variable, for example: *ptr;

  5. What is a null pointer in C? A null pointer is a pointer that does not point to any valid memory location. It is represented in C by the value 0 or NULL.

  6. How do you use pointers to dynamically allocate memory in C? You can use the malloc() function to dynamically allocate memory in C, and then use a pointer to access the allocated memory, for example: int ptr = (int) malloc(sizeof(int));

  7. How do you pass pointers as function arguments in C? You can pass pointers as function arguments by declaring the function parameter as a pointer and then passing the memory address of the variable as an argument, for example: void myFunction(int *ptr);

  8. What is a pointer arithmetic in C? Pointer arithmetic in C involves manipulating the memory address stored in a pointer variable using arithmetic operations, such as addition or subtraction.

  9. What is a void pointer in C? A void pointer is a special type of pointer that can point to any type of data. It is useful for generic programming and dynamic memory allocation.

  10. How do you use pointers to manipulate arrays in C? You can use pointers to manipulate arrays in C by using pointer arithmetic to access array elements, for example: int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; printf("%d", *(ptr + 2)); // prints 3

Pointers are an important concept in C programming that allow programmers to directly manipulate memory. A pointer is a variable that stores a memory address, which can be used to access the value stored at that memory location. To declare a pointer variable in C, you add an asterisk (*) before the variable name. For example, to declare a pointer to an integer variable, you would write:
arduino
int *ptr;
To access the value pointed to by a pointer, you use the dereference operator (*). For example, if ptr is a pointer to an integer variable, you can access the value of that variable like this:
perl
int num = 5; int *ptr = # printf("%d", *ptr); // prints 5
Pointers are commonly used for dynamic memory allocation, which allows programs to allocate memory at runtime rather than at compile-time. The malloc() function is used to allocate memory dynamically, and returns a pointer to the allocated memory. For example, to allocate memory for an integer variable, you would write:
c
int *ptr = (int*) malloc(sizeof(int));
Pointer arithmetic allows programmers to manipulate the memory address stored in a pointer variable using arithmetic operations. For example, to access the third element of an array using a pointer, you can use the following code:
perl
int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; printf("%d", *(ptr + 2)); // prints 3
Pointers can also be passed as function arguments, allowing functions to modify variables outside of their scope. To pass a pointer as an argument to a function, you declare the function parameter as a pointer, and pass the address of the variable as an argument. For example:
arduino
void myFunction(int *ptr);
Overall, understanding pointers is essential for efficient memory management and dynamic memory allocation in C programming.